/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package atm.charts;

import com.alee.laf.panel.WebPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.CategoryLineAnnotation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;

/**
 *
 * @author Quark
 */
public class XYBarChartPanel extends WebPanel {
    
    final double[][] data = new double[][] {
            // [M, T, W, T, F, S, S]
            {1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 1.0}, // 10s
            {5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 5.0}, // 20s
            {4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 2.0}, // 50s
            {8.0, 6.0, 0.0, 1.0, 3.0, 5.0, 7.0}  // 100s
        };
    
    public XYBarChartPanel() {
        CategoryDataset dataset = createDataset();
        
        ChartPanel chartPanel = new ChartPanel(createChart(dataset), false);
        chartPanel.setOpaque(false);
        
        this.add(chartPanel);
    }
    
    private CategoryDataset createDataset() {
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                //dataset.addValue(data[i][j], i, j);
            }
        }
        
        return dataset;
    }
    
    private JFreeChart createChart(final CategoryDataset dataset) {
        final JFreeChart chart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.VERTICAL, true, false, false);
        chart.setBackgroundPaint(null);
        
        chart.getLegend().setBackgroundPaint(null);
        chart.getLegend().setBorder(BlockBorder.NONE);
        chart.getLegend().setPosition(RectangleEdge.TOP);
        //chart.getLegend().setHorizontalAlignment(HorizontalAlignment.RIGHT);
        
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setForegroundAlpha(0.7f);
        plot.setBackgroundPaint(null);
        plot.setOutlineVisible(false);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinesVisible(false);
        
        // lines from M to S: can be used to draw minima for easier comparision
        /*plot.addAnnotation(new CategoryLineAnnotation("M", 5.0, "S", 5.0, Color.red, new BasicStroke(0.5f)));
        plot.addAnnotation(new CategoryLineAnnotation("M", 8.0, "S", 8.0, Color.green, new BasicStroke(0.5f)));
        plot.addAnnotation(new CategoryLineAnnotation("M", 7.0, "S", 7.0, Color.blue, new BasicStroke(0.5f)));
        plot.addAnnotation(new CategoryLineAnnotation("M", 4.0, "S", 4.0, Color.yellow, new BasicStroke(0.5f)));
        */
        BarRenderer renderer = new BarRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setShadowVisible(false);
        renderer.setBarPainter(new StandardBarPainter());
        plot.setRenderer(renderer);
        
        final CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
        domainAxis.setLowerMargin(0.0);
        domainAxis.setUpperMargin(0.0);
        domainAxis.setTickMarksVisible(true);
        domainAxis.setMinorTickMarksVisible(true);
        
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setLabelAngle(0 * Math.PI / 2.0);
        
        return chart;
    }
}
